//
//  HomeIMGView.m
//  HiCar
//
//  Created by 丁一 on 2018/12/26.
//  Copyright © 2018 丁一. All rights reserved.
//

#import "HomeImgView.h"
#import "HomeIMGviewCell.h"

@interface HomeIMGView ()<UICollectionViewDataSource,UICollectionViewDelegate>{
    CGRect   framehere;
}

@property (nonatomic, strong) UICollectionView           *collectionview;
@property (nonatomic, strong) NSMutableArray             *IMG_array;
@property (nonatomic, strong) UICollectionViewFlowLayout *fallLayout;

@end

@implementation HomeIMGView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        
        framehere = frame;
        
        if (!_collectionview) {
            _IMG_array                          = [NSMutableArray new];
            
            _fallLayout                         = [[UICollectionViewFlowLayout alloc] init];
            _fallLayout.scrollDirection         = UICollectionViewScrollDirectionVertical;
            _fallLayout.itemSize                = CGSizeMake(frame.size.width, frame.size.height);
            _fallLayout.minimumInteritemSpacing = 0;
            _fallLayout.minimumLineSpacing      = 0;
            
            _collectionview                     = [[UICollectionView alloc] initWithFrame:CGRectMake(15, 0, frame.size.width-30, frame.size.height) collectionViewLayout:_fallLayout];
            
            [_collectionview setDelegate:self];
            [_collectionview setDataSource:self];
            [_collectionview setShowsHorizontalScrollIndicator:NO];
            [_collectionview setShowsVerticalScrollIndicator:NO];
            [_collectionview setBackgroundColor:[UIColor whiteColor]];
            _collectionview.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
            [_collectionview registerClass:[HomeIMGviewCell class] forCellWithReuseIdentifier:@"HomeIMGviewCell"];
        }
        [self addSubview:_collectionview];
        
    }
    return self;
}

-(void)setArray:(NSArray *)array{
    [_IMG_array removeAllObjects];
    [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [self->_IMG_array addObject:obj];
    }];
    
    CGFloat Lin_width = (RECRWITCH-40)/3;
    _fallLayout.itemSize  = CGSizeMake(Lin_width, Lin_width);
    
    [_collectionview reloadData];
}

#pragma mark---UICollectionViewDelegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (_IMG_array.count>2) {
        return 3;
    }
    return _IMG_array.count;
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HomeIMGviewCell *cell = (HomeIMGviewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"HomeIMGviewCell" forIndexPath:indexPath];
    [cell.logo sd_setImageWithURL:[NSURL URLWithString:[_IMG_array objectAtIndex:indexPath.row]] placeholderImage:[UIImage imageNamed:@"morentu.png"]];
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    _selectimg(indexPath.row);
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor       = [UIColor clearColor];
}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return YES;
}

- (void)collectionView:(UICollectionView *)collectionView  didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor       = [UIColor clearColor];
}

@end

